package com.runchinaup.smartbelt.view.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.runchinaup.smartbelt.R;
import com.wx.wheelview.adapter.ArrayWheelAdapter;
import com.wx.wheelview.widget.WheelView;

import java.util.ArrayList;

/**
 * Created by liber on 2018/2/2.
 */

public class AlarmPop implements View.OnClickListener {

    private Context context;
    private View contentView;
    private TextView leftItem;
    private TextView rightItem;
    private WheelView hourView;
    private WheelView minuteView;

    private ClickCallback clickCallback;

    public AlarmPop(Context context) {
        this.context = context;
        initViewEvent(context);
    }

    public PopupWindow popupWindow;


    public AlarmPop showPicker(View view, String time, ClickCallback clickCallback) {

        this.clickCallback = clickCallback;
        popupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        backgroundAlpha(0.5f);

        String[] getTime = time.split(":");

        hourView.setSelection(Integer.valueOf(getTime[0]));
        minuteView.setSelection(Integer.valueOf(getTime[1]));

        return this;
    }


    private void initViewEvent(Context context) {

        contentView = LayoutInflater.from(context).inflate(R.layout.pop_alarm, null);
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setAnimationStyle(R.style.anim_menu_bottomBar);
        ColorDrawable colorDrawable = new ColorDrawable();
        colorDrawable.setAlpha(128);
        popupWindow.setBackgroundDrawable(colorDrawable);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (clickCallback != null) {
                    clickCallback.onDismiss();
                    backgroundAlpha(1f);
                }
            }
        });


        //选择取消和确定的按钮
        leftItem = contentView.findViewById(R.id.leftItem);
        leftItem.setOnClickListener(this);
        rightItem = contentView.findViewById(R.id.rightItem);
        rightItem.setOnClickListener(this);

        //选择小时和分钟
        hourView = contentView.findViewById(R.id.hourView);
        hourView.setWheelAdapter(new ArrayWheelAdapter(context));
        hourView.setSkin(WheelView.Skin.Holo);
        hourView.setWheelSize(5);
        hourView.setLoop(false);
        hourView.setWheelData(loadHour);


        minuteView = contentView.findViewById(R.id.minuteView);
        minuteView.setWheelAdapter(new ArrayWheelAdapter(context));
        minuteView.setSkin(WheelView.Skin.Holo);
        minuteView.setWheelSize(5);
        minuteView.setLoop(true);
        minuteView.setWheelData(loadMinute);


    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((FragmentActivity) context).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((FragmentActivity) context).getWindow().setAttributes(lp);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.leftItem:
                dismiss();
                if (clickCallback != null) {
                    clickCallback.onDismiss();
                }
                break;
            case R.id.rightItem:
                dismiss();
                if (clickCallback != null) {
                    clickCallback.onSelect(hourView.getCurrentPosition(), minuteView.getCurrentPosition());
                }
                break;
            default:
                return;
        }

    }

    public static abstract class ClickCallback {
        public void onDismiss() {

        }

        public abstract void onSelect(int hour, int minute);
    }

    public void dismiss() {
        popupWindow.dismiss();
        backgroundAlpha(1f);
    }

    //小时数组
    static ArrayList<String> loadHour = new ArrayList<>();

    static {
        for (int i = 0; i < 24; i++) {
            loadHour.add(String.format("%02d", i));
        }
    }

    //分钟数组
    static ArrayList<String> loadMinute = new ArrayList<>();

    static {
        for (int i = 0; i < 60; i++) {
            loadMinute.add(String.format("%02d", i));
        }
    }

}
